package org.phalanx.client.mapeditor;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.phalanx.map.MapTile;
import org.phalanx.map.MapTileSet;
import org.phalanx.zip.ZipFileArchiveReader;

public class PhalanxMapTileSetViewer extends JPanel implements
		ListSelectionListener {
	private PhalanxMapEditorPanel masterPanel;
	private JScrollPane scroller;
	private JList tileLister;
	private MapTileSet currentSet;
	private Hashtable<String, Image> cacheTable;
	private MapTile selectedMapTile;

	public PhalanxMapTileSetViewer(PhalanxMapEditorPanel parentPanel) {
		this.masterPanel = parentPanel;
		initializePanel();
	}

	private void initializePanel() {
		this.setBackground(Color.black);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		createComponents();

		tileLister.addListSelectionListener(this);

		this.add(scroller);
	}

	public void valueChanged(ListSelectionEvent arg0) {
		JList thisJList = (JList) arg0.getSource();
		selectedMapTile = (MapTile) (thisJList).getSelectedValue();
	}

	private void createComponents() {
		/* tileLister.setmo */
		tileLister = new JList();
		tileLister.setCellRenderer(new MapTileCellRenderer());
		tileLister.setBackground(Color.black);

		scroller = new JScrollPane(tileLister);
		scroller
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroller
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	}

	public void setCurrentMapTileSet(MapTileSet t) {
		currentSet = t;
		if (currentSet != null) {
			cacheTable = new Hashtable<String, Image>();

			Vector<MapTile> v = new Vector<MapTile>();

			Hashtable<String, MapTile> tileTable = currentSet
					.getMapTileSetTable();

			Enumeration<String> e = tileTable.keys();

			while (e.hasMoreElements()) {
				String tileRefName = e.nextElement();
				MapTile newTile = tileTable.get(tileRefName);
				String fileName = newTile.getImageReference();
				try {
					ZipFileArchiveReader z = new ZipFileArchiveReader();
					z.setZipFileByName(currentSet.getTileSetArchiveFile());

					byte[] imageBytes = z.getArchivedFile(fileName);
					BufferedImage newImage = ImageIO
							.read(new ByteArrayInputStream(imageBytes));
					cacheTable.put(fileName, newImage.getScaledInstance(48, 48,
							0));

					ImageIcon i = new ImageIcon(newImage);

					v.add(newTile);
				} catch (IOException ie) {
				}
			}

			tileLister.setListData(v);
		}

	}

	public MapTile getSelectedMapTile() {
		return selectedMapTile;
	}

	class MapTileCellRenderer extends DefaultListCellRenderer {

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			JLabel tileLabel = new JLabel();
			JPanel cont = new JPanel();

			if (currentSet != null && value instanceof MapTile) {
				MapTile t = (MapTile) value;

				cont.setLayout(new FlowLayout(FlowLayout.LEFT));
				tileLabel.setForeground(Color.white);
				tileLabel.setText(currentSet.getTerrainRefNameFromTile(t));
				tileLabel.setIcon(new ImageIcon(cacheTable.get(t
						.getImageReference())));
				cont.add(tileLabel);
			}

			// highlight selected item

			cont.setBackground(isSelected ? new Color(102, 0, 0) : Color.black);

			/*
			 * tileLabel.setForeground(isSelected ? Color.black : Color.white);
			 */

			return cont;
		}

	}
}
